package com.study.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @Author huang
 * @Date 2022/1/2 23:44
 * @Version 1.0
 */
public class HdfsClient {
    private FileSystem fileSystem;

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        //连接集群namenode的地址
        URI uri = new URI("hdfs://hadoop102:8020");
        //创建一个配置文件
        Configuration configuration = new Configuration();
        //获取客户端对象
        fileSystem = FileSystem.get(uri, configuration, "hmc");
    }

    @After
    public void close() throws IOException {
        //关闭资源
        fileSystem.close();
    }

    @Test
    public void testMkdir() throws URISyntaxException, IOException, InterruptedException {
        //创建目录
        fileSystem.mkdirs(new Path("/xiyou/huoguoshan1"));
    }

    /**
     * 配置文件优先级（从低到高）
     * hdfs-default.xml => hdfs-site.xml => resources目录下面的配置文件 => configuration配置
     *
     * @throws IOException
     */
    @Test
    public void testUpdate() throws IOException {
        fileSystem.copyFromLocalFile(false, false, new Path("D:\\生活\\study\\hadoop\\系统\\hadoop102.bat"), new Path("/xiyou/huoguoshan1/hadoop102.sh"));
    }

    @Test
    public void testDel() throws IOException {
        //删除文件和空目录
        fileSystem.delete(new Path("/xiyou/huoguoshan1/hadoop102.sh"), false);
        //删除非空目录
        fileSystem.delete(new Path("/xiyou/huoguoshan1"), true);
    }

    @Test
    public void testDownload() throws IOException {
        fileSystem.copyToLocalFile(false, new Path("/xiyou/huoguoshan1/hadoop102.sh"), new Path("D:\\生活\\study\\hadoop\\系统\\"), true);
    }

    /**
     * 文件的更名和移动
     */
    @Test
    public void testMV() throws IOException {
        fileSystem.rename(new Path("/xiyou/huoguoshan1/hadoop103.sh"), new Path("/xiyou/hadoop102.sh"));
    }

    @Test
    public void fileDetail() throws Exception {
        RemoteIterator<LocatedFileStatus> list = fileSystem.listFiles(new Path("/xiyou"), true);
        while (list.hasNext()) {
            LocatedFileStatus fileStatus = list.next();
            System.out.println(fileStatus.getPath());
            System.out.println(fileStatus.getLen());
        }
    }

    @Test
    public void testFile() throws IOException {
        RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/xiyou"), true);
        while (listFiles.hasNext()) {
            LocatedFileStatus file = listFiles.next();
            if (file.isFile())
                System.out.println(file.getPath() + "\t 是文件");
            else
                System.out.println(file.getPath() + "\t 是目录 ");
        }
    }
}
